<?php
namespace Admin\Model;
use Think\Model;
class UsersModel extends Model{
    
    protected $_auto=array(
        //array(完成字段1，完成规则，[完成条件，附加规则]),
        array('ctime','time',1,'function'),
        array('utime','time',1,'function'),
    );
    
    protected $_validate=array(
        array('username','require','用户名不能为空',1),
        array('email','require','邮箱不能为空',1),
        array('email','email','邮箱格式不对',1),
        
        array('username','5,15','用户名的长度必须是5-15个字符',1,'length'),
        array('username','','用户名已经被占用',1,'unique'),
        array('email','','邮箱已经被注册',1,'unique'),
        
        //array('password','repassword','两次输入的密码不一样',0,'confirm')
        
        );
    
    function addUser(){
        $_POST['salt']=$this->getSalt();
        $_POST['password']=$this->md5(I('post.password'),$_POST['salt']);
        $_POST['repassword']=$this->md5(I('post.repassword'),$_POST['salt']);
        
        if($_POST['password']!=$_POST['repassword']){
            return '两次输入密码不一样';
        }
       // print_r(I('post.')); 
       if($this->create()){
           $this->add();
           
       }else{
           return $this->getError(); 
       }
        
        
    }
    
    function  editUser(){
        if($this->create()){
            $this->where(array('id'=>$_POST['id']))->save();
            
        }else{
            return $this->getError();
        }
        
    }
 //用md5进行加密   
    function md5($str,$salt){
        
        $str=md5($str).$salt;
        
        return md5($str);
        
        
    }
    
    
    function getSalt($len=8){
        $str='0123456789abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWSYZ=~!@#$%^&*';
        
        $strlength=strlen($str)-1;
        $result='';
        for($i=0;$i<$len;$i++){
            $rd=rand(0,$strlength);
            $result.=substr($str, $rd,1);
        }
       return $result; 
    }
    
}